exports.StoreUI = GL2.Node.subclass(
{
    classname: 'StoreUI',
    
    initialize: function(model, placement) 
    {
    	this._modelObj = model;
    	this._placement = placement;
    	this._isPanned = false;
    	this.touchCountEnd = 0;
    	this.touchCountStart = 0;
    },
	
	createView: function()
	{	
		if(this._modelObj.getItemId() != "1_1_0")
		{
			var tempPath = this.getBaseResourcePath();
			this._shopImagePath = tempPath + this._modelObj.getUpgradeState() + "_" + this._placement + ".png";
		}
		else
		{
			this._shopImagePath = "./Content/images/gamelayer/environmentlayer/storeui/shutter"+this._placement+".png";
		}
		//this.setStorePosition();
		
		this.addStoreImage();
		this.handleTouches();		
	},
	
	addStoreImage: function()
	{		
		this._storeSprite = GLFactory.getSprite([TCUtil.getAdjustedX(0), TCUtil.getAdjustedY(0)], 100, this._shopImagePath, [TCUtil.getAdjustedX(this.getStoreSize().getX()), TCUtil.getAdjustedY(this.getStoreSize().getY())], [0, 0]);
		//this.storeSprite = GLFactory.getSprite([TCUtil.getAdjustedX(0), TCUtil.getAdjustedY(0)], 1, this._shopImagePath, [TCUtil.getAdjustedX(100), TCUtil.getAdjustedY(100)], [0, 0]);
		this.addChild(this._storeSprite);
	},
	
	getStoreImage: function()
	{
		if(this._storeSprite)
		{
			return this._storeSprite;
		}
		return null;
	},
	
	handleTouches: function() 
	{
		this._touchTarget = new GL2.TouchTarget();
		this._touchTarget.setSize(TCUtil.getAdjustedX(this.getStoreSize().getX()),TCUtil.getAdjustedY(this.getStoreSize().getY()));
		this._touchTarget.setAnchor(0, 0);
		this._touchTarget.setPosition(TCUtil.getAdjustedX(0), TCUtil.getAdjustedY(0));
		this._touchTarget.getTouchEmitter().addListener(MMGameLayer.getMallContainer().getMallUI()._touchHandler, this.onTouch.bind(this),10);
		//this._touchTarget.getTouchEmitter().addListener(listner, this.onTouch,111);
		this._touchTarget.setDepth(100000);
		this.addChild(this._touchTarget);
	},
	
	getModelObject: function()
	{
		return this._modelObj;
	},
	
	setStorePosition: function()
	{		
		var offsetY = 33;
		var offsetX = -8;
			
		var position = new Core.Point(0, 0);
		
		var positionY = this._modelObj.getPosition().getY();
		if(this._placement)
		{
			positionY = this._placement;
		}
		var positionX = this._modelObj.getPosition().getX();
		
		var displacementX = 0;
		var displacementY = 0;
		
		var storeWidth = 90;
	
		displacementX = - offsetX;
		displacementY = positionY * storeWidth + offsetY;
		
		position.setAll( displacementY,displacementX);
		this.setPosition(TCUtil.getAdjustedX(position.getX()), TCUtil.getAdjustedY(position.getY()));			
	},
	
	
	getStorePosition: function()
	{		
		var offsetY = 33;
		var offsetX = -8;
			
		var position = new Core.Point(0, 0);
		
		var positionY = this._modelObj.getPosition().getY();
		if(this._placement)
		{
			positionY = this._placement;
		}
		var positionX = this._modelObj.getPosition().getX();
		
		var displacementX = 0;
		var displacementY = 0;
		
		var storeWidth = 90;
	
		displacementX = - offsetX;
		displacementY = positionY * storeWidth + offsetY;
		
		position.setAll( displacementY,displacementX);
		
		return position;			
	},
	
	getStoreSize: function()
	{
		var size = new Core.Point(74, 58);
		var storePlace = this._placement;//this.getStorePlace();
		
		if(storePlace == kStorePlace.kPlaceCenter)
		{
			size.setAll(76, 47);
		}
		return size;
	},
	
	getStorePlace: function()
	{
		if(this._modelObj.getPosition().getY() == 0)
		{
			return kStorePlace.kPlaceLeft;
		}
		else if(this._modelObj.getPosition().getY() == 1)
		{
			return kStorePlace.kPlaceCenter;
		}
		else if(this._modelObj.getPosition().getY() == 2)
		{
			return kStorePlace.kPlaceRight;
		}
	},
	
	getPositionRelevantToMall: function()
	{
		var position = this._modelObj.getPosition();
		var positionX = position.getX();
		var positionY = position.getY();		
		
		var position = this.getPosition();
		var parentPosition = this.getParent().getPosition();
		var myPosition = this.getPosition();
		
		var worldPositionX = parentPosition.getX() + myPosition.getX();
		var worldPositionY = parentPosition.getY() + myPosition.getY();
		var worldPosition = new Core.Vector();
		worldPosition.setAll(worldPositionX,worldPositionY);
		
		return worldPosition;
//		console.log('zaki::: st world position --> X: '+worldPosition.getX()+"    --> Y:"+worldPosition.getY());
//		console.log('zaki::: st parent position --> X: '+parentPosition.getX()+"    --> Y:"+parentPosition.getY());
//		console.log('zaki::: st my position is --> X:'+myPosition.getX()+"    --> Y:"+myPosition.getY());
//		
	},
	
	setRelevantImage: function()
	{
		if(this._modelObj.isUpgradeInProgress() || this._modelObj.isRestockInProgress() || this._modelObj.getIsStockEmpty())
		{
			this.setRenovationImage();
		}
		else
		{
			this.setNormalImage();
		}
	},
	
	setRenovationImage: function()
	{
		var renovationImagePath = "./Content/images/gamelayer/environmentlayer/storeui/renovation" + this._placement + ".png";
		this._storeSprite.setImage(renovationImagePath,[TCUtil.getAdjustedX(this.getStoreSize().getX()), TCUtil.getAdjustedY(this.getStoreSize().getY())], [0, 0]);
	},
	
	setNormalImage: function()
	{
		var tempPath = this.getBaseResourcePath();
		var shopImagePath = tempPath + this._modelObj.getUpgradeState() + "_" + this._placement + ".png";
		this._storeSprite.setImage(shopImagePath,[TCUtil.getAdjustedX(this.getStoreSize().getX()), TCUtil.getAdjustedY(this.getStoreSize().getY())], [0, 0]);
	},
	
	onTouch: function(touch)
	{
		MMGameLayer.getMallUI().getTouchHandler().onTouch(touch);
		switch(touch.getAction())
		{
			case touch.Action.Start:
				this.touchCountStart = this.touchCountEnd;

                return true;
			break;
            
            case touch.Action.End:
            {				
                if(!this._isPanned || this.touchCountEnd < (this.touchCountStart + 5))
                {
	                var storeContainer = this.getParent();
					storeContainer.storeTapped(this);
                }
                this._isPanned = false;
            }
			break;
			
            
            case touch.Action.Move:
            	this.touchCountEnd++;	

				this._isPanned = true;
				//MMGameLayer.getMallUI().getTouchHandler().onTouch(touch);
			break;
		}		
	},
	
//	touchEnd: function()
//	{
// 		if(!this._isPanned)
//        {
//            var storeContainer = this.getParent();
//			storeContainer.storeTapped(this);
//        }
//        this._isPanned = false;		
//	},
	
	getBaseResourcePath: function()
	{
		var tempPath = this._modelObj.getItemId().split("_")
		
		return "./Content/images/storeimages/" + tempPath[0] + "/" + tempPath[1] + "/" + tempPath[2] + "/" +"default/";
	},
								   
	destroy: function()
    {
		
    },
});
